package com.xuecheng.content.controller;

import com.xuecheng.api.content.TeachplanApi;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.api.content.model.dto.TeachplanMediaDTO;
import com.xuecheng.api.content.model.vo.BindTeachplanMediaVO;
import com.xuecheng.api.content.model.vo.TeachplanVO;
import com.xuecheng.common.domain.code.CommonErrorCode;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.TeachplanConvert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.TeachplanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 课程计划 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping
public class TeachplanController implements TeachplanApi {

    @Autowired
    private TeachplanService  teachplanService;

    @GetMapping("teachplan/{courseBaseId}/tree-nodes")
    public TeachplanDTO queryTreeNodesByCourseId(@PathVariable Long courseBaseId) {
        //获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        if (ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(CommonErrorCode.E_100108);
        }
        return teachplanService.queryTreeNodesByCourseId(courseBaseId, companyId);
    }

    @PostMapping("teachplan")
    public TeachplanDTO createOrModifyTeachPlan(@RequestBody TeachplanVO vo) {
        //获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        //vo-->dto
        TeachplanDTO dto = TeachplanConvert.INSTANCE.vo2dto(vo);
        return teachplanService.createOrModifyTeachPlan(dto, companyId);
    }

    @DeleteMapping("teachplan/{teachPlanId}")
    public void removeTeachPlan(@PathVariable Long teachPlanId) {

        //获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        //调用service
        teachplanService.removeTeachPlan(teachPlanId, companyId);
    }

    @PostMapping("teachplan/media/association")
    public TeachplanMediaDTO associateMedia(@RequestBody BindTeachplanMediaVO vo) {
        //获得公司id
        Long companyId = UAASecurityUtil.getCompanyId();
        // 2.构建 dto 数据
        TeachplanMediaDTO dto = new TeachplanMediaDTO();
        dto.setMediaId(vo.getMediaId());
        dto.setTeachplanId(vo.getTeachplanId());
        // 3.调用service 方法
        return teachplanService.associateMedia(dto, companyId);
    }
}
